#!/usr/bin/env python
# coding=utf-8

import os
import sys
import re
import codecs
import logging
import pyExcelerator as pE


xlsFileName = r'D:\ylb_work\data\je609_japan.xls'
headFileName = r'D:\ylb_work\data\waveID_Japan.h'
logFileName = r'makeconst_Japan.log'


sheets = range(1,6)
startLineNo = 9
maxLineNo = 10000

def init_log():
    logger = logging.getLogger()
    hdlr = logging.FileHandler(logFileName)
    formatter = logging.Formatter('%(asctime)s %(levelname)s:: %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)
    logger.info('\n\n\n\n\n')
    return logger
logf = init_log()

def check_line(s, lino):
	for row in range(6):
		if not s.has_key((lino, row)):
			return False
	return True


def write_header(xlsfn, fnout):
	xls = pE.parse_xls(xlsfn)
	fout = codecs.open(fnout, 'w', 'utf-8')
	for s in sheets:
		lcount = 0
		stc = xls[s][1]
		fout.write('\n\n\n' + r'// ' + xls[s][0] + '\n')
		for lino in range(startLineNo, maxLineNo+1):
			if check_line(stc, lino):
				lcount = lcount + 1
				line = 'const DWORD %s = \t%s;\t\t// %s \n' % (stc[(lino,2)], stc[(lino,1)], stc[(lino,3)], )
				fout.write(line)
		logf.info('Sheet(%s) has %d constants. ', xls[s][0], lcount)


def main():
	write_header(xlsFileName, headFileName)

if __name__ == '__main__':
	main()



